﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Security;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;

namespace CardReader.Controls
{
  [SecuritySafeCritical]
  class CardOperate
  {
    public int made_kind, ic_kind, biao_kind, BuyGas, AlarmGas, maxadd;
    public string port, baud;
    public StringBuilder id;
    //是否整数
    public bool IsInt(string s)
    {
      bool flag = false;
      if (string.IsNullOrEmpty(s))
      {
        return flag;
      }
      try
      {
        Int32.Parse(s);
        flag = true;
      }
      catch
      {
        flag = false;
      }
      return flag;
    }
    //读卡
    public int CardRead()
    {
      int a = IC.ArReadInfo(Convert.ToInt32(port), Convert.ToInt32(baud), id, out made_kind, out ic_kind, out biao_kind,
        out BuyGas, out AlarmGas, out maxadd);
      return a;
    }
    //操作结果
    public void Alert(int a)
    {
      if (a == 0)
      {
        MessageBox.Show("操作成功");
      }
      else if (a == -1)
      {
        MessageBox.Show("打开端口错");
      }
      else if (a == -2)
      {
        MessageBox.Show("没有插卡");
      }
      else if (a == -4)
      {
        MessageBox.Show("非153或24卡");
      }
      else if (a == -100)
      {
        MessageBox.Show("写卡失败");
      }
      else if (a == -3)
      {
        MessageBox.Show("卡加电错");
      }
    }
    //初始化
    public CardOperate()
    {
      port = GetXMLValue("/ic/port");
      baud = GetXMLValue("/ic/baud");
      id = new StringBuilder();
    }
    //获取xml节点值
    public string GetXMLValue(string node)
    {
      string path = Assembly.GetExecutingAssembly().Location;
      path = path.Substring(0, path.LastIndexOf('\\'));//删除文件名
      string value = "";
      XmlDocument dom = new XmlDocument();
      dom.Load(path + "\\IC.xml");//装载XML文档 
      XmlNode xnode = dom.SelectSingleNode(node);
      value = xnode.InnerText;

      return value;
      //string sPort = node.Attributes["com"].Value;
      //node = dom.SelectSingleNode("/ic/baud");
    }
    //检查是否存在
    public bool Check()
    {
      bool flag = true;
      if (port == "")
      {
        MessageBox.Show("port节点不存在");
        return false;
      }
      if (baud == "")
      {
        MessageBox.Show("baud节点不存在");
        return false;
      }
      return flag;
    }
    //获取客户信息
    public DataSet GetKHXX(string bs)
    {
      string sql = "select * from jcxx_khxx where kh=@0";
      SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20) };
      cmdParams[0].Value = bs;
      DataSet ds = DbHelperSQL.Query(sql, cmdParams);
      return ds;
    }
    //获取上次抄表时间
    public DataSet GetSCCB(string sKHID, string sRQ)
    {
      string sSql = @"select a.*,b.cbr,convert(varchar,cbsj,23) rq from SJLR_CBSJ a,jcxx_cbr b
where a.cbrid=b.cbrid and KHID=@0 and cbsj=
(select max(cbsj) cbsj from sjlr_cbsj where khid=@0 and cbsj<=@1)";
      SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20), new SqlParameter("@1", SqlDbType.VarChar, 20) };
      cmdParams[0].Value = sKHID;
      cmdParams[1].Value = sRQ;
      DataSet ds = DbHelperSQL.Query(sSql, cmdParams);
      return ds;
    }
    //付款方式
    public DataSet GetFKDS()
    {
      string sSql = "select ROW_NUMBER() OVER (ORDER BY fkid desc) AS xh,* from jcxx_fkfs";
      DataSet ds = DbHelperSQL.Query(sSql);
      return ds;
    }
    //抄表人
    public DataSet GetCBRDS()
    {
      string sSql = "select ROW_NUMBER() OVER (ORDER BY cbrid desc) AS xh,* from jcxx_cbr";
      DataSet ds = DbHelperSQL.Query(sSql);
      return ds;
    }
    //操作员
    public DataSet GetCZYDS()
    {
      //string sSql = "select a.dlm,a.xm from sys_user a,sys_yhgn b where a.dlm=b.dlm and b.xtgnid=5";
      //DataSet ds = DbHelperSQL.Query(sSql);
      //DataRow dr = ds.Tables[0].NewRow();
      //dr["dlm"] = "";
      //dr["xm"] = "";
      //ds.Tables[0].Rows.InsertAt(dr, 0);
      //return ds;
      DataSet ds = new DataSet();
      DataTable dt = new DataTable();
      ds.Tables.Add(dt);
      ds.Tables[0].Columns.Add("dlm", typeof(string));
      ds.Tables[0].Columns.Add("xm", typeof(string));
      XmlDocument dom = new XmlDocument();
      dom.Load("IC.xml");//装载XML文档 
      XmlNode xn = dom.SelectSingleNode("/ic/czr");
      XmlAttributeCollection xac = xn.Attributes;
      DataRow dr = ds.Tables[0].NewRow();
      dr["dlm"] = "";
      dr["xm"] = "";
      ds.Tables[0].Rows.Add(dr);
      foreach (XmlAttribute xa in xac)
      {
        dr = ds.Tables[0].NewRow();
        dr["dlm"] = xa.Name;
        dr["xm"] = xa.Value;
        ds.Tables[0].Rows.Add(dr);
      }
      return ds;
    }
  }
}
